09. Always Moving
Always Moving
Always Moving
Self-driving cars constantly monitor their state. So, movement and localization have to occur in parallel.
If we use a Kalman filter for localization, this means that as a car moves, the Kalman filter has to keep coming up with new state estimates. This way, the car always has an idea of where it is.
Always Predicting State
In the code below, you are given a predict_state
function that takes in a current state and a change in time, dt
, and returns the new state estimate (based on a constant velocity model).
It will be up to you to use this function repeatedly to find the predicted_state at 5 different points in time:
- the initial state
- the predicted state after 2 seconds have elapsed
- the predicted state after 3 more seconds have elapsed
- the predicted state after 1 more second has elapsed
- the predicted state after 4 more seconds have elapsed
To first three states have been given to you in code.
Start Quiz:
from functions import predict_state
# predict_state takes in a state and a change in time, dt
# So, a call might look like: new_state = predict_state(old_state, 2)
# The car starts at position = 0, going 60 m/s
# The initial state:
initial_state = [10, 60]
# After 2 seconds:
state_est1 = predict_state(initial_state, 2)
# 3 more seconds after the first estimated state
state_est2 = predict_state(state_est1, 3)
## TODO: Use the predict_state function
## and the above variables to calculate the following states
## (And change their value from 0 to the correct state)
## Then, click Test Run to see your results!
## 1 more second after the second state estimate
state_est3 = 0
## 4 more seconds after the third estimated state
state_est4 = 0
#---- predict state function --#
def predict_state(state, dt):
# Assumes a valid state had been passed in
# Assumes a constant velocity model
x = state[0]
new_x = x+state[1]*dt
# Create and return the new, predicted state
predicted_state = [new_x, state[1]]
return predicted_state
from functions import predict_state
# predict_state takes in a state and a change in time, dt
# So, a call might look like: new_state = predict_state(old_state, 2)
# The car starts at position = 0, going 60 m/s
# The initial state:
initial_state = [10, 60]
# After 2 seconds:
state_est1 = predict_state(initial_state, 2)
# 3 more seconds after the first estimated state
state_est2 = predict_state(state_est1, 3)
## Use the predict_state function
## and the above variables to calculate the following states
## 1 more second after the second state estimate
state_est3 = predict_state(state_est2, 1)
## 4 more seconds after the third estimated state
state_est4 = predict_state(state_est3, 4)
User's Answer:
(Note: The answer done by the user is not guaranteed to be correct)
from functions import predict_state
# predict_state takes in a state and a change in time, dt
# So, a call might look like: new_state = predict_state(old_state, 2)
# The car starts at position = 0, going 60 m/s
# The initial state:
initial_state = [10, 60]
# After 2 seconds:
state_est1 = predict_state(initial_state, 2)
# 3 more seconds after the first estimated state
state_est2 = predict_state(state_est1, 3)
## TODO: Use the predict_state function
## and the above variables to calculate the following states
## (And change their value from 0 to the correct state)
## Then, click Test Run to see your results!
## 1 more second after the second state estimate
state_est3 = predict_state(state_est2, 1)
## 4 more seconds after the third estimated state
state_est4 = predict_state(state_est3, 4)
#---- predict state function --#
def predict_state(state, dt):
# Assumes a valid state had been passed in
# Assumes a constant velocity model
x = state[0]
new_x = x+state[1]*dt
# Create and return the new, predicted state
predicted_state = [new_x, state[1]]
return predicted_state
from functions import predict_state
# predict_state takes in a state and a change in time, dt
# So, a call might look like: new_state = predict_state(old_state, 2)
# The car starts at position = 0, going 60 m/s
# The initial state:
initial_state = [10, 60]
# After 2 seconds:
state_est1 = predict_state(initial_state, 2)
# 3 more seconds after the first estimated state
state_est2 = predict_state(state_est1, 3)
## Use the predict_state function
## and the above variables to calculate the following states
## 1 more second after the second state estimate
state_est3 = predict_state(state_est2, 1)
## 4 more seconds after the third estimated state
state_est4 = predict_state(state_est3, 4)